package com.shanjupay.common.utilbak;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 
 * 时间工具类
 * @see
 * @since
 */
public class TimeUtils {

	/**
	 * 时间格式YYYY-MM-DD HH:mm:ss
	 */
	public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 时间格式YYYY-MM-DD HH:mm
	 */
	public static final String TIME_FORMAT_MIN = "yyyy-MM-dd HH:mm";
	
	/**
	 * 时间格式YYYY-MM-DD H:mm
	 */
	public static final String TIME_FORMAT_H_MIN = "yyyy-MM-dd H:mm";

	/**
	 * 日期格式YYYY-MM-DD
	 */
	public static final String DATE_FORMAT = "yyyy-MM-dd";

	/**
	 * 时分秒格式 HH:mm:ss
	 */
	public  static  final String TIME_FORMAT_TIME = "HH:mm:ss";

	/**
	 * HH:mm
	 */
	public  static  final String TIME_FORMAT_TIME_HOUR = "HH:mm";

	/**
	 * 将日期转换为字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToString(Date date, String format) {
		if (date == null) {
			return null;
		}
		String dateStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 将字符串转换为日期
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date stringToDate(String dateStr, String format) {
		if (dateStr == null || dateStr.equals(""))
			return null;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将制定对象转换为日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date stringToDate(Object date, String format) {
		if (date instanceof String) {
			return TimeUtils.stringToDate((String) date, format);
		}
		return null;
	}

	/**
	 * 获取指定日期最小时间
	 * 
	 * @param now
	 * @return
	 */
	public static Date getMinDate(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取指定日期最大时间
	 * 
	 * @param now
	 * @return
	 */
	public static Date getMaxDate(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.HOUR, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	/**
	 * 获取最小时间字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getMinDateString(String dateStr) {
		if (dateStr == null || "".equals(dateStr) || dateStr.length() == 19) {
			return dateStr;
		}
		return dateStr + " 00:00:00";

	}

	/**
	 * 将字符串转换为最小时间
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date getMinDate(String dateStr) {
		return getMinDate(stringToDate(dateStr, "yyyy-MM-dd"));
	}

	/**
	 * 获取最大日期字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getMaxDateString(String dateStr) {
		if (dateStr == null || "".equals(dateStr) || dateStr.length() == 19) {
			return dateStr;
		}
		return dateStr + " 23:59:59";
	}

	/**
	 * 将字符串转换为最大时间
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date getMaxDate(String dateStr) {

		return getMaxDate(stringToDate(dateStr, "yyyy-MM-dd"));
	}

	/**
	 * 获取指定日期下一个月的日期
	 * 
	 * @param now
	 * @return
	 */
	public static Date getNextMonthDate(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取指定日期前一个月的日期
	 * 
	 * @param now
	 * @return
	 */
	public static Date getPreMonthDate(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
		return cal.getTime();
	}

	/**
	 * 获取指定日期前一日的日期
	 * 
	 * @param now
	 * @return
	 */
	public static Date getPreDate(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 1);
		return cal.getTime();
	}

	/**
	 * 获取指定日期后一日的日期
	 * 
	 * @param now
	 * @return
	 */
	public static Date getNextDate(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
		return cal.getTime();
	}

	/**
	 * 获取指定日期后若干个月的日期
	 * 
	 * @param now
	 *            指定日期
	 * @param num
	 *            月
	 * @return
	 */
	public static Date getEndMonthDate(Date now, int num) {	
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + num);
		return cal.getTime();
	}

	/**
	 * 将日期字符转换为另一种格式的字符串
	 * 
	 * @param dateStr
	 *            日期字符串
	 * @param srcFormat
	 *            初始格式
	 * @param tagFormat
	 *            目标格式
	 * @return
	 */
	public static String convertDateFormat(String dateStr, String srcFormat,
			String tagFormat) {
		Date date = stringToDate(dateStr, srcFormat);
		return dateToString(date, tagFormat);
	}

	/**
	 * 获取当前日期的字符串
	 * 
	 * @param format
	 *            日期字符串
	 * 
	 * @return 指定格式的日期字符串
	 */
	public static String getDateString(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	/**
	 * 获取当前日期的字符串
	 * 
	 * @return 指定格式的日期字符串
	 */
	public static String getDateString() {
		return getDateString("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 获得指定年月的天数<br>
	 * Description:
	 * 
	 * @param year
	 *            指定的年
	 * @param month
	 *            指定的月
	 * @return
	 * @since <IVersion>
	 */
	public static int getMonthDays(int year, int month) {
		Calendar timex = Calendar.getInstance();
		timex.clear();
		timex.set(Calendar.YEAR, year); // year 为 int
		timex.set(Calendar.MONTH, month - 1);// 注意,Calendar对象默认一月为0
		return timex.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 比较两个时间相差的天数<br>
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 * @since <IVersion>
	 */
	public static Long compareDay(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
	}

	/**
	 * 得到订单的时间格式<br>
	 * 
	 * @return
	 * @since <IVersion>
	 */
	public static String getOrderNum() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date);
	}

	/**
	 * 获取日期，格式：yyyy-MM-dd HH:mm:ss<br>
	 * 
	 * @return
	 * @since <IVersion>
	 */
	public static String getDateFormatter() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	/**
	 * 获取日期，格式：yyyyMMdd<br>
	 * 
	 * @return
	 * @since <IVersion>
	 */
	public static String getDate() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}

	/**
	 * 
	 * 获取当前年份和季度的字符串
	 * 
	 * @return
	 * @since <IVersion>
	 */
	public static String getBeforeYearQuarter() {
		Calendar c = Calendar.getInstance();
		String quarter = "01";
		int month = c.get(Calendar.MONTH);
		if (month >= 4 && month < 7) {
			quarter = "02";
		} else if (month >= 7 && month < 10) {
			quarter = "03";
		} else if (month >= 10 && month <= 12) {
			quarter = "04";
		}
		return c.get(Calendar.YEAR) + quarter;
	}

	/**
	 * 获取当月最大天数
	 * 
	 * @return
	 */
	public static int getCurrentMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				1);
		calendar.roll(Calendar.DATE, false);
		return calendar.get(Calendar.DATE);
	}

	/**
	 * * *
	 * 
	 * @param endDate 结束日期
	 *            <String> *
	 * @param startDate 开始日期
	 *            <String> *
	 * @return int *
	 * @throws ParseException
	 */
	public static int calculateMonthIn(Date endDate, Date startDate) {
		Calendar cal1 = new GregorianCalendar();
		cal1.setTime(endDate);
		Calendar cal2 = new GregorianCalendar();
		cal2.setTime(startDate);
		int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12
				+ cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
		return c;
	}
	
	/**
     * 获取年份列表
     */
	@SuppressWarnings("deprecation")
	public static List<Integer> getYearList(int startYear){
	    List<Integer> yearList = new ArrayList<Integer>();
    	int year = new Date().getYear() + 1900;
        for(int i=startYear;i<=year;i++){
        	yearList.add(i);
        }
        return yearList;
	}
	
	/** 
	    * 获取过去第几天的日期 
	    * 
	    * @param past 
	    * @return 
	    */  
	   public static String getPastDate(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       String result = format.format(today);  
	       return result;  
	   }  
	  
	   /** 
	    * 获取未来 第 past 天的日期 
	    * @param past 
	    * @return 
	    */  
	   public static String getFetureDate(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       String result = format.format(today);  
	       return result;  
	   } 
	   
	   /**
	    * 判断两个时间相差的毫秒数
	   * @Title: getIntervalTime 
	   * @return long    返回类型 
	   * @author zengwenbin 	
	   * @date 2017-8-9 下午1:46:24	 
	   * @throws
	    */
	   public static long getIntervalTime(Date now,Date f){
		   long interval = (now.getTime() - f.getTime())/1000;
		   return interval;
	   }
	   
	   public static Date getThisWeekMonday(Date date) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			// 获得当前日期是一个星期的第几天
			int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
			if (1 == dayWeek) {
				cal.add(Calendar.DAY_OF_MONTH, -1);
			}
			// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			// 获得当前日期是一个星期的第几天
			int day = cal.get(Calendar.DAY_OF_WEEK);
			// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
			cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
			return cal.getTime();
		}
	   
	   public static Date getThisMonthOneDay(Date date){
		   Calendar c = Calendar.getInstance();    
		   c.add(Calendar.MONTH, 0);
		   c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天   
		   return c.getTime();
	   }

	   public static String dateAsISO(String dateString){
		   //data转化成ISO8601格式时间
		   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		   LocalDateTime dt1 = LocalDateTime.parse(dateString,formatter);
		   ZoneOffset offset = ZoneOffset.of("+08:00");
		   OffsetDateTime date = OffsetDateTime.of(dt1,offset);

		   DateTimeFormatter formatter2=DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
		   String nowAsISO  =date.format(formatter2);
		   return nowAsISO;
	   }

	public static void main(String[] args) {
			
		//System.out.println(calculateMonthIn( new Date(),stringToDate("2016-02-31",DATE_FORMAT)));
		
		//System.out.println(getIntervalTime(new Date(),TimeUtils.stringToDate("2017-08-09 13:49:00", TIME_FORMAT)));
		//System.out.println(TimeUtils.getEndMonthDate(new Date(), 24));
		/*System.out.println(TimeUtils.getMinDateString(TimeUtils.getDateString(DATE_FORMAT)));
		System.out.println(TimeUtils.getMinDateString(TimeUtils.dateToString(TimeUtils.getThisWeekMonday(new Date()), TimeUtils.DATE_FORMAT)));
		System.out.println(TimeUtils.getMinDateString(TimeUtils.dateToString(TimeUtils.getThisMonthOneDay(new Date()), TimeUtils.DATE_FORMAT)));*/
		
		//System.out.println(getMaxDate(getPreDate(new Date())));
		Date now = new Date();
		String checkInMinTime = "20:00";
		Date checkInMinTimeD = stringToDate((dateToString(now,TimeUtils.DATE_FORMAT)+ " "+ checkInMinTime),DateUtil.FORMAT_STANDARD_MIN);
		
		String checkInMaxTime = "7:10";
		//Date checkInMaxTimeD = new Date(checkInMaxTime);
		
		//System.out.println(new Date().getHours());
		//System.out.println(stringToDate((dateToString(TimeUtils.getNextDate(now),TimeUtils.DATE_FORMAT)+ " "+ checkInMinTime),DateUtil.FORMAT_STANDARD_MIN));
		
		//System.out.println(dateToString(checkInMinTimeD, DateUtil.FORMAT_STANDARD_MIN));

		System.out.println(TimeUtils.getDateFormatter());
		System.out.println(dateAsISO(TimeUtils.getDateFormatter()));
	}

}
